package com.itboye.eweilisten.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.Homebean;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.widget.XImageLoader;

import java.text.DecimalFormat;

/**
 * Created by itboye on 2017/3/7.
 */

public class HomeAdapter extends BaseAdapter {
    Context context;
    Homebean list;

    public HomeAdapter(Context context, Homebean list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.getBook().size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHouder houder;
        if (convertView == null) {
            houder = new ViewHouder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_home_fragment, null);
            houder.img = (ImageView) convertView.findViewById(R.id.imgBook);
            houder.tvEename = (TextView) convertView.findViewById(R.id.tvEename);
            houder.tvjianjie = (TextView) convertView.findViewById(R.id.tvjianjie);
            houder.tvedPrice = (TextView) convertView.findViewById(R.id.tvedPrice);
            convertView.setTag(houder);
        } else {
            houder = (ViewHouder) convertView.getTag();
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        double douPrice = Double.parseDouble(list.getBook().get(position).getMin_price());
        houder.tvEename.setText(list.getBook().get(position).getName());
        houder.tvjianjie.setText(list.getBook().get(position).getSynopsis());
        String price = df.format(douPrice / 100);
        houder.tvedPrice.setText("¥"+price + "");
        Log.d("imgmgimg", list.getBook().get(position).getMain_img() + "");
        XImageLoader.load(Const.IMAGE_HEAD + list.getBook().get(position).getMain_img(), houder.img);
        return convertView;
    }
    public class ViewHouder {
        private ImageView img;
        private TextView tvEename;
        private TextView tvjianjie;
        private TextView tvedPrice;
    }
}
